package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.customer.domain.CommonAddresses;
import com.ktgj.customer.domain.vo.CommonReimbursementsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.CommonReimbursementsMapper;
import com.ktgj.customer.domain.CommonReimbursements;
import com.ktgj.customer.service.ICommonReimbursementsService;

/**
 * 常用报销凭证信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class CommonReimbursementsServiceImpl implements ICommonReimbursementsService 
{
    @Autowired
    private CommonReimbursementsMapper commonReimbursementsMapper;

    /**
     * 查询常用报销凭证信息
     * 
     * @param commonReimbursementsId 常用报销凭证信息ID
     * @return 常用报销凭证信息
     */
    @Override
    public CommonReimbursements selectCommonReimbursementsById(Long commonReimbursementsId)
    {
        return commonReimbursementsMapper.selectCommonReimbursementsById(commonReimbursementsId);
    }

    /**
     * 查询常用报销凭证信息列表
     * 
     * @param commonReimbursements 常用报销凭证信息
     * @return 常用报销凭证信息
     */
    @Override
    public List<CommonReimbursements> selectCommonReimbursementsList(CommonReimbursements commonReimbursements)
    {
        return commonReimbursementsMapper.selectCommonReimbursementsList(commonReimbursements);
    }

    /**
     * 新增常用报销凭证信息
     * 
     * @param commonReimbursements 常用报销凭证信息
     * @return 结果
     */
    @Override
    public int insertCommonReimbursements(CommonReimbursements commonReimbursements)
    {
        return commonReimbursementsMapper.insertCommonReimbursements(commonReimbursements);
    }

    /**
     * 修改常用报销凭证信息
     * 
     * @param commonReimbursements 常用报销凭证信息
     * @return 结果
     */
    @Override
    public int updateCommonReimbursements(CommonReimbursements commonReimbursements)
    {
        return commonReimbursementsMapper.updateCommonReimbursements(commonReimbursements);
    }

    /**
     * 批量删除常用报销凭证信息
     * 
     * @param commonReimbursementsIds 需要删除的常用报销凭证信息ID
     * @return 结果
     */
    @Override
    public int deleteCommonReimbursementsByIds(Long[] commonReimbursementsIds)
    {
        return commonReimbursementsMapper.deleteCommonReimbursementsByIds(commonReimbursementsIds);
    }

    /**
     * 删除常用报销凭证信息信息
     * 
     * @param commonReimbursementsId 常用报销凭证信息ID
     * @return 结果
     */
    @Override
    public int deleteCommonReimbursementsById(Long commonReimbursementsId)
    {
        return commonReimbursementsMapper.deleteCommonReimbursementsById(commonReimbursementsId);
    }

    /**
     * 我的-新增报销凭证信息
     * */
    @Override
    public int saveCommonReimbursementsByCustomerId(CommonReimbursements commonReimbursements) {
        commonReimbursements.setIsDeletes("false");
        return commonReimbursementsMapper.insertCommonReimbursementsByCustomerId(commonReimbursements);
    }

    /**
     * 查询待编辑的报销凭证
     * */
    @Override
    public CommonReimbursements certificateToBeModified(Long commonReimbursementsId) {
        return commonReimbursementsMapper.certificateToBeModified(commonReimbursementsId);
    }

    /**
     * 修改报销凭证
     * */
    @Override
    public int updateCommonReimbursementsByCustomerId(CommonReimbursements commonReimbursements) {
        if (commonReimbursements.getInvoiceType() != null && commonReimbursements.getInvoiceType().equals("personal")){
            commonReimbursements.setIdentificationNumber(null);
            commonReimbursements.setRegisteredAddress(null);
            commonReimbursements.setPhone(null);
            commonReimbursements.setBankName(null);
            commonReimbursements.setBankAccount(null);
        }
        return commonReimbursementsMapper.updateCommonReimbursements(commonReimbursements);
    }

    /**
     * 根据会员id查询常用报销凭证列表
     * */
    @Override
    public List<CommonReimbursementsVo> selectCommonReimbursements(Long memberId, String invoiceType) {
        List<CommonReimbursements> commonReimbursementsList = commonReimbursementsMapper.selectCommonReimbursements(memberId);
        List<CommonReimbursementsVo> commonReimbursementsVoList = new ArrayList<>();
        for (CommonReimbursements commonReimbursements : commonReimbursementsList) {
            if(invoiceType.equals("") || invoiceType==null){
                if(commonReimbursements.getInvoiceType().equals("company")){
                    CommonReimbursementsVo commonReimbursementsVo = new CommonReimbursementsVo();
                    commonReimbursementsVo.setInvoiceHeadsUp(commonReimbursements.getInvoiceHeadsUp());
                    commonReimbursementsVo.setCommonReimbursementsId(commonReimbursements.getCommonReimbursementsId());
                    commonReimbursementsVo.setIdentificationNumber(commonReimbursements.getIdentificationNumber());
                    commonReimbursementsVo.setRegisteredAddress(commonReimbursements.getRegisteredAddress());
                    commonReimbursementsVo.setBankName(commonReimbursements.getBankName());
                    commonReimbursementsVo.setWhetherToDefault(commonReimbursements.getWhetherToDefault());
                    commonReimbursementsVoList.add(commonReimbursementsVo);
                }else if(commonReimbursements.getInvoiceType().equals("personal") ){
                    CommonReimbursementsVo commonReimbursementsVo = new CommonReimbursementsVo();
                    commonReimbursementsVo.setCommonReimbursementsId(commonReimbursements.getCommonReimbursementsId());
                    commonReimbursementsVo.setInvoiceHeadsUp(commonReimbursements.getInvoiceHeadsUp());
                    commonReimbursementsVo.setWhetherToDefault(commonReimbursements.getWhetherToDefault());
                    commonReimbursementsVoList.add(commonReimbursementsVo);
                }

            }

            if (invoiceType.equals("company") && commonReimbursements.getInvoiceType().equals(invoiceType)){
                CommonReimbursementsVo commonReimbursementsVo = new CommonReimbursementsVo();
                commonReimbursementsVo.setInvoiceHeadsUp(commonReimbursements.getInvoiceHeadsUp());
                commonReimbursementsVo.setCommonReimbursementsId(commonReimbursements.getCommonReimbursementsId());
                commonReimbursementsVo.setIdentificationNumber(commonReimbursements.getIdentificationNumber());
                commonReimbursementsVo.setRegisteredAddress(commonReimbursements.getRegisteredAddress());
                commonReimbursementsVo.setBankName(commonReimbursements.getBankName());
                commonReimbursementsVo.setWhetherToDefault(commonReimbursements.getWhetherToDefault());
                commonReimbursementsVoList.add(commonReimbursementsVo);
            }
            else if (invoiceType.equals("personal") && commonReimbursements.getInvoiceType().equals(invoiceType)){
                CommonReimbursementsVo commonReimbursementsVo = new CommonReimbursementsVo();
                commonReimbursementsVo.setCommonReimbursementsId(commonReimbursements.getCommonReimbursementsId());
                commonReimbursementsVo.setInvoiceHeadsUp(commonReimbursements.getInvoiceHeadsUp());
                commonReimbursementsVo.setWhetherToDefault(commonReimbursements.getWhetherToDefault());
                commonReimbursementsVoList.add(commonReimbursementsVo);
            }
        }
        return commonReimbursementsVoList;
    }

    @Override
    public List<CommonReimbursements> purchaseQueryCommon(Long customerId) {
        List<CommonReimbursements> commonReimbursements = commonReimbursementsMapper.selectCommonReimbursements(customerId);
        if (commonReimbursements != null && commonReimbursements.size() > 0) {
            commonReimbursements.forEach(item -> {
                if (item.getBankName() != null && !item.getBankName().equals("") && item.getBankAccount() != null && !item.getBankAccount().equals(""))
                    item.setBankName(item.getBankName() + item.getBankAccount().substring(item.getBankAccount().length() - 4));
            });
        }
        return commonReimbursements;
    }

    /**
     * 删除发票信息
     * */
    @Override
    public int deleteCommonReimbursements(Long commonReimbursementsId) {
        int i = commonReimbursementsMapper.deleteCommonReimbursements(commonReimbursementsId);
        return i;
    }

    /**
     * id查信息
     * */
    @Override
    public CommonReimbursements selectCommonReimbursementsBycommonReimbursementsId(Long commonReimbursementsId) {
        CommonReimbursements commonReimbursements = commonReimbursementsMapper.selectCommonReimbursementsBycommonReimbursementsId(commonReimbursementsId);
        return commonReimbursements;
    }
}
